<link type="text/css" rel="stylesheet" href="__PUBLIC__/css/validator.css"/>
<script type="text/javascript" src="__PUBLIC__/js/checkuser.js"></script>
<style>
    .close{font-size:34px;font-weight:400;color:#fff;}
    .close:hover{color:#fff;}
    input::-webkit-outer-spin-button,input::-webkit-inner-spin-button{
        -webkit-appearance:textfield;
    }
    input[type="number"]{
        -moz-appearance:textfield;
    }
</style>
<div class="modal-header" style="padding:3px 10px;">
    <b style="font-size:16px;">新增回款记录</b>
    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
</div>
<div class="modal-body clearfix">
                    <form id="ad_Form" class="form-horizontal m-t" method="post">
                        <input type="hidden" name="customer_id" id="customer_id" value="">
                        <div class="col-sm-12">
                            <div class="form-group">
                                <div class="col-sm-1">
                                </div>
                                <div class="col-sm-7">
                    <select name="business_id" id="business" class="form-control checkit" rell="对应项目" onchange="contractChanged();">
                        <option value="">--回款对应项目--</option>
                        <volist name="business" id="vo">
                            <option value="{$vo['business_id']}">{$vo['business']}</option>
                        </volist>
                    </select>
                </div>
                <div class="col-sm-3 error_msg" id="businessTip"></div>
            </div>
            <div class="form-group">
                <div class="col-sm-1"></div>
                <label class="control-label col-sm-3" style="width:110px;"><span style="color:#FF0011;">*</span>回款对应客户:</label>
                <div class="col-sm-6">
                    <input type="text" style="width: 100%;" name="customer" id="customer" class="form-control" value="" readonly="readonly" >
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-1">
                </div>
                <label class="control-label col-sm-3" style="width:110px;"><span style="color:#FF0011;">*</span>对应计划期次:</label>
                <div class="col-sm-6">
                    <select name="num" style="width: 100%;" id="nu" rell="期次" onchange="getnu();" class="form-control checkit">
                        <option value="" id="first" selected="selected">0</option>
                    </select>
                </div>
                <div class="col-sm-3 error_msg" id="nuTip"></div>
            </div>
            <div class="form-group">
                <div class="col-sm-3">
                </div>
                <div class="col-sm-6">
                    <select name="type" id="period" rell="回款类型" onchange="" style="width: 100%;" class="form-control checkit">
                        <option value="" selected="selected">回款类型</option>
                        <option value="1" >预付款</option>
                        <option value="2" >首付款</option>
                        <option value="3" >过保付款</option>
                        <option value="4" >慧猎系列款</option>
                    </select>
                </div>
                <div class="col-sm-3 error_msg" id="periodTip"></div>
            </div>
            <div class="form-group">
                <div class="col-sm-1">
                </div>
                <label class="control-label col-sm-3" style="width:110px;"><span style="color:#FF0011;">*</span>回款日期：</label>
                <div class="col-sm-6">
                    <input type="date" id="date" rell="回款日期" name="date" class="form-control checkit">
                </div>
                <div class="col-sm-3 error_msg" id="dateTip"></div>
            </div>
            <div class="form-group">
                <div class="col-sm-1">
                </div>
                <label class="control-label col-sm-3" style="width:110px;"><span style="color:#FF0011;">*</span>回款金额：</label>
                <div class="col-sm-6">
                    <input id="money" rell="回款金额" name="money" type="number" class="form-control checkit money number">
                </div>元
                <div class="col-sm-3 error_msg" id="moneyTip"></div>
            </div>
            <div class="form-group">
                <div class="col-sm-1">
                </div>
                <label class="control-label col-sm-3" style="width:110px;"><span style="color:#FF0011;">*</span>付款方式：</label>
                <div class="col-sm-6">
                    <input id="method" rell="付款方式" name="method" type="text" class="form-control checkit">
                </div>
                <div class="col-sm-3 error_msg" id="methodTip"></div>
            </div>
            <div class="form-group">
                <div class="col-sm-1">
                </div>
                <label class="control-label col-sm-3" style="width:110px;"><span style="color:#FF0011;">*</span>收款人：</label>
                <div class="col-sm-6">
                    <input id="payee" rell="收款人" name="payee" type="text" class="form-control checkit">
                </div>
                <div class="col-sm-3 error_msg" id="payeeTip"></div>
            </div>
            <div class="form-group">
                <div class="col-sm-1">
                </div>
                <label class="control-label col-sm-3" style="width:110px;">备注：</label>
                <div class="col-sm-6">
                    <textarea class="form-control" name="remark" rows="3"></textarea>
                </div>
            </div>

        </div>
    </form>
</div>
<script>
    function beforecheck() {
        $(".checkit").each(function () {
            var id = "#"+$(this).attr('id')+"Tip";
            if($(this).val()==''){
                $(id).html("<span style='color: red;'>*此项为必填项</span>");
                return false;
            }else {
                $(id).html("");
                return true;
            }
        });
        $(".number").each(function () {
            var te = parseFloat($(this).val()) ;
            var id = "#"+$(this).attr('id')+"Tip";
            if(isNaN(te)){
                $(id).html("<span style='color: red;'>*请填写数字</span>");
                return false;
            }
            else  {
                $(id).html("");
                return true;
            }
        });
        if($("#num").val()==0) {
            $("#numTip").html("<span style='color: red;'>*请选择期次或其他项目</span>");
            return false;
        }else {
            $("#numTip").html("");
            return true;
        }
    }

    function contractChanged() {
        var bvalue = $("#business").val();
        $.ajax({
            type: "post",
            async:false,
            url: "{:U('return/contractChanged')}",
            data: {business_id:bvalue},
            dataType: "json",
            success : function(result){
                $("#first").nextAll().remove();
                $("#customer").val(result.customer_name);
                $("#business_id").val(result.business_id);
                $("#customer_id").val(result.customer_id);
                // var nums = result.nums;
                var html = "";
                // for(var i = 1;i<=nums ;i++){
                //     html += "<option value='"+i+"'>"+i+"</option>";
                // }
                result.num.forEach(function (v,k,array) {
                    html += "<option value='"+v.num+"'>"+v.num+"</option>";
                });
                $("#first").after(html);
            }
        });
    }

    $(document).ready(function(){
        $("#adbtn").click(function(){
            if(beforecheck() && before_submit()){
                    swal({
                            title: "您确定要保存此回款记录吗？",
                            type: "warning",
                            showCancelButton: true,
                            confirmButtonColor: "#DD6B55",
                            confirmButtonText: "是的，我要保存！",
                            cancelButtonText:'让我再考虑一下…',
                            closeOnConfirm:false,
                            closeOnCancel:false
                        },
                        function(isConfirm){
                            if (isConfirm) {
                                $.ajax({
                                    type: "POST",
                                    url: "{:U('record_add')}",
                                    data:$("#ad_Form").serialize(),
                                    async: true,
                                    success: function(data) {
                                        if(data.status == 1){
                                            swal({
                                                title: "添加成功！",
                                                text: "跳转中!",
                                                type: "success"
                                            });
                                            $('#Modal').modal('hide');
                                            top.location.reload();
                                        }else{
                                            swal({
                                                title: "添加失败!",
                                                text: data.info,
                                                type: "error"
                                            });
                                        }
                                    }
                                });
                            } else {
                                swal("已取消","您取消了添加操作！","error");
                            }
                        })
            }else{
                var item_id = [];
                $('.checkit').each(function(k,v){
                    checkform(v);
                    item_id.push($(v).attr('id'));
                });
                $.each(item_id,function(k,v){
                    if($('#'+v+'Tip').html() == ''){
                        item_id.splice(k,1);
                    }
                });
                $('#'+item_id[0]).focus();
                return false;
            }
        });

    });

</script>
<div class="modal-footer" style="padding:8px 22px;">
    <button type="button" id="adbtn" class="btn btn-primary">保存</button>
    <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
</div>